﻿using Meiam.System.Common;
using Meiam.System.Model;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meiam.System.Interfaces
{
    public class CheckDeviceInfoService: SqlSugarMysqlBase<dt_device_info>, ICheckDeviceInfoService
    {
        public CheckDeviceInfoService(IConfiguration _configuration)
        {
            base.connectionString = _configuration.GetConnectionString("DB4");
        }

        public async Task<bool> Run(string param)
        {
            try
            {
                var devicelist = base.GetWhere(r => r.DEVTABLE_NAME == "" && r.DEVICE_STATUS == -1&&r.RESOURCE_ID!=null).ToList();
                await Task.Run(() =>
                {
                    foreach (var device in devicelist)
                    {
                        device.DEVICE_STATUS = 2;
                        device.LAST_TIME = DateTime.Now;
                    }
                    
                    base.Update(devicelist);

                    string offlinemachineno = string.Empty;
                    devicelist.ForEach(machine =>
                    {
                        if (devicelist.Count >= 2)
                        {
                            offlinemachineno += $"{machine.DEVICE_DESC}" + ",";
                        }
                        else
                        {
                            offlinemachineno = machine.DEVICE_DESC;
                        }
                    });

                    if (!string.IsNullOrEmpty(offlinemachineno))
                    {
                        OAPushHelper.Push("505", "SCADA设备停止信息","","", param, offlinemachineno.TrimEnd(','));
                    }
                });

            }
            catch(Exception ex)
            {
                Console.WriteLine("SCADA设备状态检测失败,原因:" + ex.Message);
            }

            return true;

        }
    }
}
